
package com.song.tradition.page.splash

import android.content.Context
import androidx.compose.animation.core.*
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.twotone.KeyboardArrowRight
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.PagerState
import com.google.accompanist.pager.rememberPagerState
import com.google.accompanist.systemuicontroller.SystemUiController
import com.google.accompanist.systemuicontroller.rememberSystemUiController

import com.song.tradition.R
import com.song.tradition.entity.SplashData
import com.song.tradition.ui.theme.*
import kotlinx.coroutines.launch


@OptIn(ExperimentalPagerApi::class)
@Composable
fun SplashScreen(
    navController: NavHostController

) {
    val systemUiController: SystemUiController = rememberSystemUiController()
//    不显示statusBar
    systemUiController.isStatusBarVisible = false
    val context = LocalContext.current
    val items = arrayOf(
        SplashData(R.drawable.splash01, "    人有六欲，药有七情，本草相遇，或君臣佐使，或相生相克，而真正的故事，也许就从这一刻开始"),
        SplashData(R.drawable.splash02, "    中医本草，本非是一成不变的存在，千百年来，人们在与疾病的抗争中，也常有新的发现和认知"),
        SplashData(R.drawable.splash03, "    天人合一，医道相通，自古以来，中医药就与道教哲学有着密不可分的渊源")
    )

    val pagerState = rememberPagerState(
        initialPage = 0
    )
    OnBoardingPage(
        item = items,
        pagerState = pagerState,
        modifier = Modifier.fillMaxWidth(),

        ) {

        val sharedPreference =  context.getSharedPreferences("APP", Context.MODE_PRIVATE)
        val editor = sharedPreference.edit()
        editor.putBoolean("isFirst",false)
        editor.apply()


//        context.startActivity(Intent(context, MainActivity::class.java))

    }

}

@OptIn(ExperimentalPagerApi::class)
@Composable
fun OnBoardingPage(
    item: Array<SplashData>,
    pagerState: PagerState,
    modifier: Modifier,
    handleClick: () -> Unit,

    ) {
    Box(modifier = modifier.background(Color.White)) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            HorizontalPager(state = pagerState, count = 3) { page ->

                Column(
                    modifier = Modifier
                        .fillMaxSize(),
                    verticalArrangement = Arrangement.Top,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Image(
                        painter = painterResource(id = item[page].imgUrl),
                        contentDescription = "",
                        modifier = Modifier.fillMaxWidth()
                    )
                }


            }
        }
        Box(modifier = Modifier.align(Alignment.BottomCenter)) {
            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(350.dp),
                shape = RoundedCornerShape(topStart = 40.dp),
            ) {

                Box {
                    Column(
                        modifier = Modifier
                            .fillMaxSize()
                            .background(background100)
                            .padding(horizontal = 16.dp),
                        horizontalAlignment = Alignment.CenterHorizontally,

                        ) {
                        PagerIndicator(items = item, currentPage = pagerState.currentPage)
                        Spacer(modifier = Modifier.height(16.dp))
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .weight(1f)
                        ) {
                            Text(
                                text = item[pagerState.currentPage].text,
                                fontFamily = FontFamily.SansSerif,
                                fontWeight = FontWeight.ExtraLight,
                                lineHeight = 30.sp,
                                fontSize = 20.sp,
                                modifier = Modifier.align(Alignment.Center)
                            )
                        }
                        Spacer(modifier = Modifier.height(16.dp))
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceBetween,
                            verticalAlignment = Alignment.Bottom
                        ) {

                            val width = animateDpAsState(
                                targetValue = if (item.size - 1 == pagerState.currentPage) 200.dp else 56.dp,
                                animationSpec = tween(200, 0, easing = FastOutLinearInEasing)
                            )
                            TextButton(onClick = { /*TODO*/ }) {
                                Text(text = "跳过", color = Color.Gray)
                            }
                            val coroutineScope = rememberCoroutineScope()


                            Button(
                                onClick = {
                                    if (item.size - 1 == pagerState.currentPage) {
                                        handleClick.invoke()

                                    } else {
                                        coroutineScope.launch {
                                            pagerState.animateScrollToPage(pagerState.currentPage + 1)
                                        }
                                    }
                                },
                                modifier = Modifier
                                    .width(width.value)
                                    .height(56.dp)
                                    .clip(CircleShape),
                                colors = ButtonDefaults.buttonColors(containerColor= customCardBgColor)
                            ) {
                                if (item.size - 1 == pagerState.currentPage) {
                                    Text(text = "开启中医药文化之旅", color = Color.White)
                                }

                                Icon(
                                    imageVector = Icons.TwoTone.KeyboardArrowRight,
                                    contentDescription = "",
                                    tint = Color.White
                                )
                            }
                        }
                        Spacer(modifier = Modifier.height(16.dp))

                    }
                }
            }
        }
    }
}

@Composable
fun PagerIndicator(items: Array<SplashData>, currentPage: Int) {
    Row(
        horizontalArrangement = Arrangement.SpaceBetween,
        modifier = Modifier.padding(top = 16.dp)
    ) {
        repeat(items.size) {
            Indicator(isSelected = it == currentPage)
        }
    }
}

@Composable
fun Indicator(isSelected: Boolean, color: Color = primary100) {
    val width = animateDpAsState(targetValue = if (isSelected) 32.dp else 8.dp)

    Box(
        modifier = Modifier
            .padding(4.dp)
            .height(8.dp)
            .width(width.value)
            .clip(CircleShape)
            .background(if (isSelected) color else background300)
    )
}
